{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组： [0, 1, 0, 3, 12]\n",
      "移动后数组： [1, 3, 12, 0, 0]\n"
     ]
    }
   ],
   "source": [
    "from typing import List\n",
    "\n",
    "class solution:\n",
    "    def moveZeros(self, nums: List[int]) -> None:\n",
    "        \"\"\"\n",
    "        实现将数组中所有的0移动到数组末尾\n",
    "        \"\"\"\n",
    "        n = len(nums)\n",
    "        left = right = 0\n",
    "        while right < n:\n",
    "            # 从左向右依次找到非零元素\n",
    "            if nums[right] != 0:\n",
    "                nums[left], nums[right] = nums[right], nums[left]\n",
    "                # 左侧有序数列长度加1\n",
    "                left += 1\n",
    "            # 继续向后遍历\n",
    "            right += 1\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    nums = [0, 1, 0, 3, 12]\n",
    "    print(\"原始数组：\", nums)\n",
    "    s = solution()\n",
    "    s.moveZeros(nums)\n",
    "    print(\"移动后数组：\", nums)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pylearn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
